644B - Processing Queries - CodeForces Solution


*special problem constructive algorithms data structures two pointers *1700

Please click on ads to support us..

Python Code:

from queue import Queue


if __name__ == '__main__':
    n, b = list(map(int, input().split()))
    q = Queue()
    next_avail = 0
    task_list = []
    for i in range(n):
        t, d = list(map(int, input().split()))
        task = [t, d, 0]
        task_list.append(task)
    
    start = 0
    t0, d0, f0 = task_list[0]
    next_avail = t0 + d0        
    task_list[0][2] = next_avail
    
    q.put(next_avail)
    
    for i in range(1, n):
        temp_task = task_list[i]
        while (q.qsize() > 0) and (temp_task[0] >= q.queue[0]):
            q.get()
        
        if next_avail <= temp_task[0]:
            next_avail = temp_task[0] + temp_task[1]
            temp_task[2] = next_avail
            q.put(next_avail)
        else:
            if q.qsize() > b:
                temp_task[2] = -1
            else:
                next_avail += temp_task[1]
                temp_task[2] = next_avail
                q.put(next_avail)
            
        
    for t in task_list:
        print(t[2], end=" ")

C++ Code:

#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

int main() {
	int n, b;
	cin >> n >> b;
    b++;

	vector<int> t(n);
	vector<int> d(n);
    vector<long long> ans(n);

	for (int i = 0; i < n; i++) {
		cin >> t[i] >> d[i];
	}
	
	queue<int> q;
	q.push(0);
	long long l = t[0];
	long long r = t[0] + d[0];
	int sz = 1;

    for (int i = 1; i < n; i++) {

        while (!q.empty() && l + d[q.front()] <= t[i]) {
			int c = q.front();
			q.pop();

			l += d[c];
			ans[c] = l;
			sz--;
		}

        if (r > t[i]) {
			if (sz < b) {
				q.push(i);
				sz++;
				r += d[i];
			}
			else {
				ans[i] = -1;
			}
		}

        else if (r <= t[i]) {
			while (!q.empty()) {
				int c = q.front();
				q.pop();

				l += d[c];
				ans[c] = l;
			}

			sz = 1;
			q.push(i);
			l = t[i];
			r = t[i] + d[i];
		}

	}

    while (!q.empty()) {
		int c = q.front();
		q.pop();

		l += d[c];
		ans[c] = l;
	}
    
	for (int i = 0; i < ans.size(); i++) {
		cout << ans[i] << " ";
	}
}/*1690754475.6171336*/


Comments

Submit
0 Comments
More Questions

39F - Pacifist frogs
1451C - String Equality
386A - Second-Price Auction
1690E - Price Maximization
282B - Painting Eggs
440A - Forgotten Episode
233B - Non-square Equation
628B - New Skateboard
262B - Roma and Changing Signs
755C - PolandBall and Forest
456B - Fedya and Maths
376B - IOU
1623B - Game on Ranges
1118A - Water Buying
1462C - Unique Number
301A - Yaroslav and Sequence
38A - Army
38C - Blinds
1197A - DIY Wooden Ladder
1717D - Madoka and The Corruption Scheme
1296D - Fight with Monsters
729D - Sea Battle
788A - Functions again
1245B - Restricted RPS
1490D - Permutation Transformation
1087B - Div Times Mod
1213B - Bad Prices
1726B - Mainak and Interesting Sequence
1726D - Edge Split
1726C - Jatayu's Balanced Bracket Sequence